#include "include.h"
#include "func.h"
#include "func_music.h"
#include "func_exspiflash_demo.h"
#include "api_spiflash_ex.h"

#if (EX_SPIFLASH_SUPPORT)
#define MAX_PRV_SONG 5
#define MAX_CUR_SONG 2
#define MAX_AFTER_SONG 5
#define MAX_DEMO_TIMEOUT_TIME 5



#define MAX_TIMEOUT_TIME 30
#define SONG_INT_NUM 10
#define POWER_ON_SONG_NUM 3

demo_msc_t demo_msc AT(.buf.func.demo);
u16 demo_play_time=0;
u32 demo_song_ticks = 3;
AT(.text.func.demo.table)
const demo_play_time_info_t play_time_info[2]=
{
	{216,332,0x259,0x15180},//
	{216,332,0x259,0x15180},//
};
AT(.text.func.demo.table)
const u8 fade_in_out_falg[16]=
{
	2,4,6,8,10,12,14,16,18,20
};
AT(.text.func.demo.table)
const u8 fade_in_out_1_falg[10]=
{
	3,6,9,12,15,18,21,24,27,30
};

AT(.text.func.demo.table)
const u8 demo_song_table[4]=
{
	1,2,6,4
};
AT(.text.func.demo.table)
const u8 song_end_time_table[2]=
{
	250,250,
};
AT(.text.func.music.table)
const song_time_play_t demo_song1_time[]=
{
	{0,LED_SEQUENCE_7}, 	//1
	{101,LED_SEQUENCE_3},
	{3239,LED_SEQUENCE_4},
	{3374,LED_SEQUENCE_13},
};


bool demo_exspiflash_init(void)
{
    printf("exspiflash_init\n");
    spiflash1_init();
    if (!is_exspiflash_online()) {
        return false;
    }
    return true;
}
AT(.text.func.demo)
void demo_set_song_play_num(void)
{
	u8 led_mode;
	music_control(MUSIC_MSG_STOP);
	demo_msc.pause=false;
	amp_speaker_unmute();
	if(sys_cb.power_on_song_play)
	{
		demo_msc.cur_num=POWER_ON_SONG_NUM;
		led_mode=LED_SEQUENCE_1;
	}
	else if(demo_msc.song_int_flag)
	{
		demo_msc.cur_num=SONG_INT_NUM;
		led_mode=LED_SEQUENCE_4;
	}
	else
	{
		demo_msc.cur_num=demo_song_table[demo_msc.song_index];
		if(demo_msc.song_index==3)
		{
			led_mode=LED_SEQUENCE_9;
		}
		else if(demo_msc.song_index==2)
		{
			led_mode=LED_SEQUENCE_11;
		}
		else
		{
			msc_breakpiont_t song_brkpt;//用于保存和恢复播放位置的结构体			
			song_brkpt.frame_count=play_time_info[demo_msc.song_index].frame_count;
			song_brkpt.file_ptr=play_time_info[demo_msc.song_index].file_ptr;
			music_set_cur_time(play_time_info[demo_msc.song_index].start_time);	
			demo_msc.vol_adjust=0;
			demo_msc.need_adjust=0;
			exspifalsh_music_num_kick(demo_msc.cur_num);
			music_set_jump(&song_brkpt);
			led_mode=LED_SEQUENCE_3;
			bsp_set_led_mode(led_mode);
			return;
			#if 0
			led_mode=demo_song1_time[0].light_mode;
			demo_msc.time_offset=1;
			demo_song_ticks=0;
			demo_play_time=0;
			#endif
		}
	}	
	exspifalsh_music_num_kick(demo_msc.cur_num);
	bsp_set_led_mode(led_mode);
}
AT(.text.func.demo)
void demo_check_song_play(void)
{
	if (get_music_dec_sta() == MUSIC_STOP)
	{
		if(demo_msc.pause)
		{
			return;
		}
		if(demo_msc.song_int_flag)
		{
			demo_msc.pause=true;
			demo_msc.timeout_cnt=0;
			demo_msc.song_int_flag=0;
			bsp_set_led_mode(LED_SEQUENCE_2);
			return;
		}
		if(sys_cb.power_on_song_play)
		{
			sys_cb.power_on_song_play=0;
			demo_msc.pause=true;
			mic_bias_trim();
			demo_set_song_play_num();
			return;
		}
		if(tick_check_expire(demo_msc.song_ticks,500))
		{
			demo_msc.song_ticks=tick_get();
			if(!demo_msc.song_ticks)
			{
				printf("tick delay start!\n");
				demo_msc.tick_start=1;
				return;
			}
		}
		else
		{
			return;
		}
		demo_msc.tick_start=0;
		
		if(demo_msc.song_index<=1)
		{
			if(sys_cb.project_num)
			{
				demo_msc.song_index=3;
			}
			else if(sys_cb.QR_code_disable)
			{
				demo_msc.song_index=3;
			}
			else
			{
				demo_msc.song_index=2;
			}
		}
		else
		{
			demo_msc.song_index++;
		}
		if(demo_msc.song_index>3)
		{
			demo_msc.pause=true;
			music_control(MUSIC_MSG_STOP);
			if(sys_cb.bt_connected == 1)
			{
				music_control(MUSIC_MSG_STOP);
				func_cb.sta = FUNC_BT;
				return;
			}
			else
			{
				sys_cb.pwrdwn_tone_en=0;
				func_cb.sta=FUNC_PWROFF;
			}
			return;
		}
		demo_set_song_play_num();
	}
}
AT(.text.func.music)
void demo_song_light_sync_deal(void)
{

	u16 song_play_time=0;
	u8 light_mode=0;
	if(demo_msc.pause)
	{
		return;
	}
	if(demo_msc.song_index>1)
	{
		return ;
	}
	return;
	#if 1
	if(tick_check_expire(demo_song_ticks,100))
	{
		u32 cur_time1 = music_get_cur_time();
		demo_song_ticks = tick_get();
		printf("demo play time:%d\n",cur_time1);
		if(cur_time1>=song_end_time_table[demo_msc.song_index])
		{
			if(sys_cb.project_num)
			{
				demo_msc.song_index=3;
			}
			else if(sys_cb.QR_code_disable)
			{
				demo_msc.song_index=3;
			}
			else
			{
				demo_msc.song_index=2;
			}
			demo_set_song_play_num();
		}
	}
	#else
	if(tick_check_expire(demo_song_ticks,10))
	{
		demo_song_ticks = tick_get();
		song_play_time=demo_song1_time[demo_msc.time_offset].time;
		light_mode=demo_song1_time[demo_msc.time_offset].light_mode;
		if(demo_play_time==song_play_time)
		{
			u32 cur_time1 = music_get_cur_time();
			if(demo_play_time!=(cur_time1/10))
			{
				demo_play_time=cur_time1*10;
			}
			bsp_set_led_mode(light_mode);

			demo_msc.time_offset++;
		}
		demo_play_time++;
	}
	#endif
}

AT(.text.func.demo)
void demo_switch_song(u8 key_index)
{
	#if 1
	demo_msc.timeout_cnt=0;
	if(sys_cb.power_on_song_play)
	{
		sys_cb.power_on_song_play=0;
		demo_msc.pause=true;
		mic_bias_trim();
	}
	
	if(!sys_cb.project_num)
	{
		demo_msc.song_index++;
		if(demo_msc.song_index>1)
		{
			demo_msc.song_index=0;
		}
	}
	else
	{
		if(demo_msc.pause)
		{
			demo_msc.song_index=key_index;
		}
		else if(demo_msc.song_int_flag)
		{
			demo_msc.song_int_flag=0;
			demo_msc.song_index=key_index;
		}
		else
		{
			if(key_index!=demo_msc.song_index)
			{
				demo_msc.song_index=key_index;

			}
			else
			{
				demo_msc.pause=true;
				demo_msc.song_int_flag=1;
			}

		}
	}
	demo_set_song_play_num();
	#else
	demo_msc.song_index++;
	if(demo_msc.song_index>1)
	{
		demo_msc.song_index=0;
	}
	demo_set_song_play_num();
	#endif

}
AT(.text.func.demo)
void demo_switch_mic_onoff(u8 onoff)
{
	if(!sys_cb.project_num)
	{
		return;
	}
	if(onoff)
	{
		if(sys_cb.vbat_low)
		{
			return;
		}
		sys_cb.mic_onoff=1;
		if (get_music_dec_sta() == MUSIC_PLAYING&&(demo_msc.song_index<2))
		{
		}
		else
		{
			bsp_set_led_mode(LED_SEQUENCE_7);
		}
		if(get_music_dec_sta() != MUSIC_PLAYING)
		{
			amp_speaker_mute();
		}
		bsp_karaok_init(AUDIO_PATH_KARAOK, func_cb.sta);
		if(get_music_dec_sta() != MUSIC_PLAYING)
		{
			delay_ms(300);
			amp_speaker_unmute();
		}
	}
	else
	{
		sys_cb.mic_onoff=0;
		bsp_karaok_exit(AUDIO_PATH_KARAOK);
		if(get_music_dec_sta() != MUSIC_PLAYING)
		{
			amp_speaker_mute();
			delay_ms(100);
		}
		if (get_music_dec_sta() == MUSIC_PLAYING&&(sys_cb.song_mode==2))
		{
			bsp_set_led_mode(LED_SEQUENCE_9);
		}
		else if(get_music_dec_sta() != MUSIC_PLAYING)
		{
		}
	}
}
AT(.text.func.demo)
void func_demo_lower_power_check(void)
{
	if(sys_cb.mic_onoff&&sys_cb.karaok_init)
	 {
	 	u8 mic_off;
	 	mic_off=bsp_get_mic_vol_offset();
		if(sys_cb.mic_vol_offset!=mic_off)
		{
			sys_cb.mic_vol_offset=mic_off;
			bsp_karaok_set_mic_volume();
		}
		if ((sys_cb.vbat <=2700) ||(adc_cb.vrtc_val>=0x39f))
		 {
		 	sys_cb.vbat_low=1;
			bsp_karaok_exit(AUDIO_PATH_KARAOK);
		 }
	 }
}
AT(.text.func.demo)
u8 demo_check_power_key(void)
{
	u8 key_cnt=0;
	u16 msg;
	while (1)
	{
	    WDT_CLR();
	    delay_ms(20);
		msg = msg_dequeue();
		printf("msg:0x%x\n",msg);
		if(msg==KEY_POWER||msg==KL_POWER||msg==KH_POWER)
		{
			key_cnt=10;
			break;
		}
		key_cnt++;
		if(key_cnt>6)
		{
			break;
		}
	}
	if(key_cnt>=10)
	{
		return 0;
	}
	else
	{
		return 1;
	}
}
AT(.text.func.demo)
void func_demo_check_timeout(void)
{

	if(!sys_cb.project_num)
	{
		return;
	}

	if(sys_cb.mic_onoff)
	{
		demo_msc.timeout_cnt=0;
		return;
	}
	if(demo_msc.pause)
	{
		demo_msc.timeout_cnt++;
		amp_speaker_mute();
		if(demo_msc.timeout_cnt>=demo_msc.max_timeout_cnt)
		{
			demo_msc.timeout_cnt=0;
			music_control(MUSIC_MSG_STOP);
			func_cb.sta = FUNC_PWROFF;
		}
	}
}
AT(.text.func.demo)
bool exspifalsh_demo_image_check(void)
{
    if (is_exspiflash_online()) {
        demo_msc.total_num = exspifalsh_music_get_file_total();
		printf("exspi_msc.total_num = %d\n",demo_msc.total_num);
        if (demo_msc.total_num > 0) {
            return true;
        }
    }
    return false;
}
AT(.text.func.music)
void auto_stop_play_timer(void)
{
	static u32 ticks = 0;
	u8 song_index=demo_msc.song_index;
	u16 check_time;
	u8 need_stop=false;
	if(demo_msc.pause)
	{
		return;
	}
	if(sys_cb.power_on_song_play)
	{
		return;
	}
	if(song_index>1)
	{
		return;
	}
    if(tick_check_expire(ticks,100)){      //100ms记忆一次

		ticks = tick_get();
        check_time = music_get_cur_time();
		#if 0
		msc_breakpiont_t brkpt;		
		music_get_breakpiont(&brkpt);
		printf("time:%d->frame_count:0x%x->file_ptr:0x%x\n",check_time,brkpt.frame_count,brkpt.file_ptr);
		#endif
		#if 1
		if(demo_msc.need_adjust)
		{
			printf("play time:0x%x->adjust_cnt:%d\n",check_time,demo_msc.adjust_cnt);
			if(demo_msc.vol_adjust==1)
			{
				demo_msc.adjust_cnt+=3;
				sys_cb.vol+=2;
				bsp_set_volume(bsp_volume_inc(sys_cb.vol));
				if(demo_msc.adjust_cnt>=16||sys_cb.vol==MUSIC_VOL)
				{
					demo_msc.need_adjust=0;
					demo_msc.adjust_cnt=0;
					sys_cb.vol==MUSIC_VOL;
					bsp_set_volume(sys_cb.vol);
				}
			}
			else
			{
				demo_msc.adjust_cnt++;
				bsp_set_volume(bsp_volume_dec(sys_cb.vol));
				if(demo_msc.adjust_cnt>=10||sys_cb.vol==0)
				{
					demo_msc.need_adjust=0;
					demo_msc.adjust_cnt=0;
					sys_cb.vol==0;
					bsp_set_volume(sys_cb.vol);
					need_stop=true;
				}
							
			}
		}

		//if(check_time==SONG_PLAY_END_TIME)
		if(check_time==play_time_info[song_index].end_time)
		{
			demo_msc.need_adjust=1;
			demo_msc.vol_adjust=0;
			demo_msc.adjust_cnt=0;
			printf("sys_cb.vol:%d\n",sys_cb.vol);
		}
		if(need_stop)
		{
			music_control(MUSIC_MSG_STOP);
			bsp_set_volume(MUSIC_VOL);
		}
		#endif

    }
}

AT(.text.func.demo)
static void func_exspifalsh_demo_enter(void)
{
    msg_queue_clear();
    if ((!exspifalsh_demo_image_check())){
        func_cb.sta = FUNC_BT;
        return;
    }
    func_cb.mp3_res_play = NULL;//func_exspifalsh_mp3_res_play;
    register_spi_read_function(spiflash1_read);
	//param_QR_code_disable_read();
	//bsp_set_volume(VOL_MAX);
	amp_speaker_unmute();
	bsp_set_volume(MUSIC_VOL);
    demo_msc.pause = true;
	if(!sys_cb.project_num)
	{
		sys_cb.song_mode=0;
		demo_msc.song_index=0;
	}
	else
	{
		demo_msc.song_index=sys_cb.song_mode;
	}
	printf("song mode:%d\n",sys_cb.song_mode);

	demo_msc.max_timeout_cnt=MAX_TIMEOUT_TIME;
	demo_set_song_play_num();
}
AT(.text.func.demo)
static void func_exspifalsh_demo_exit(void)
{
    music_control(MUSIC_MSG_STOP);
	sys_cb.power_on_song_play=0;
    register_spi_read_function(NULL);  //恢复内部SPI读接口.
    func_cb.last = FUNC_EXSPIFLASH_DEMO;
}

AT(.text.func.demo)
void func_exspifalsh_demo_process(void)
{
    func_process();
	demo_check_song_play();
	demo_song_light_sync_deal();
}

AT(.text.func.demo)
void func_exspifalsh_demo(void)
{
	#if 1
    printf("%s\n", __func__);
	if(demo_check_power_key()==0)
	{
		printf("key long!!\n");
		demo_msc.longkey_wakeup=1;
	}
	sys_cb.project_num=param_spiflash_project_num_read();
    func_exspifalsh_demo_enter();
    while (func_cb.sta == FUNC_EXSPIFLASH_DEMO) {
        func_exspifalsh_demo_process();
        func_exspifalsh_demo_message(msg_dequeue());
		auto_stop_play_timer();
    }
    func_exspifalsh_demo_exit();
	#endif
}
#endif

